﻿myApp.controller("BestBuyController", function ($scope, $http, $modal, ngTableParams, $routeParams, $filter, authService, $location, $window,$rootScope) {
    $scope.data.searchData = [];
    $scope.internaluser = false;
    document.title = "Penang Realty BESTBUY 世界地產 BESTBUY";
    $scope.$emit('newPageLoaded', { 'title': 'Penang Realty BESTBUY 世界地產 BESTBUY', 'description': 'Bestbuy properties you should not miss, come and see!' });
    if ($scope.authentication.userType !== "Valid User" && $scope.authentication.userType !== "") $scope.internaluser = true;

    console.log($scope.authentication.userType);

    function pageInit() {
        bestBuySearch();
    }



    $scope.tableParams = new ngTableParams({
        page: 1,            // show first page
        count: 25,           // count per page
        filter: {
            //Project: ''       // initial filter
        }

    }, {
        total: $scope.data.searchData.length, // length of data
        getData: function ($defer, params) {
            var orderedProperty = params.filter() ? $filter('filter')($scope.data.searchData, params.filter()) : $scope.data.searchData;
            // use build-in angular filter
            orderedProperty = params.sorting() ? $filter('orderBy')(orderedProperty, params.orderBy()) : orderedProperty;
            params.total(orderedProperty.length); // set total for recalc pagination
            $defer.resolve(orderedProperty.slice((params.page() - 1) * params.count(), params.page() * params.count()));         
        }
    })

    bestBuySearch = function () {
        $scope.loader.loading = true;

        if ($scope.tableParams !== undefined) $scope.tableParams.reload();

        url = 'api/PropertySearch/GetBestBuytProperty';

        var element_to_scroll_to = document.getElementById('notification');
        element_to_scroll_to.scrollIntoView();

        $http.post(url, $scope.property)
             .success(function (result) {
                 console.log(result);
                 $scope.loader.loading = false;
                 $scope.data.searchData = result;
                 window.prerenderReady = true;
                               
             })
             .error(function (result) {
                 console.log("Error");
                 console.log(result);
                 $scope.loader.loading = false;
                 window.prerenderReady = true;
             }
         )        
    }

    $scope.$watch('data.searchData', function (newValue, oldValue) {
           if ($scope.tableParams) $scope.tableParams.reload();
    });

    $scope.go = function (key, value) {
        console.log($scope.data.SaleType);
        value['transact'] = $scope.data.SaleType;
        var jsonObj = JSON.stringify(value);
        sessionStorage.setItem(key, jsonObj);
    }



    pageInit();

   
})


